/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.engine.network.handlers;

import etherblocks.engine.network.messages.Message_BlockChunk;
import com.cubes.BlockChunkControl;
import com.cubes.BlockTerrainControl;
import com.cubes.network.CubesSerializer;

/**
 *
 * @author Carl
 */
public class BlockTerrainMessageHandler{

    public BlockTerrainMessageHandler(BlockTerrainControl blockTerrain){
        this.blockTerrain = blockTerrain;
        BlockChunkControl[][][] chunks = blockTerrain.getChunks();
        chunkHandlers = new BlockChunkMessageHandler[chunks.length][chunks[0].length][chunks[0][0].length];
        for(int x=0;x<chunks.length;x++){
            for(int y=0;y<chunks[x].length;y++){
                for(int z=0;z<chunks[x][y].length;z++){
                    chunkHandlers[x][y][z] = new BlockChunkMessageHandler();
                }
            }
        }
    }
    private BlockTerrainControl blockTerrain;
    private BlockChunkMessageHandler[][][] chunkHandlers;
    
    public void onMessageReceived(Message_BlockChunk message){
        BlockChunkMessageHandler chunkHandler = chunkHandlers[message.getX()][message.getY()][message.getZ()];
        chunkHandler.onChunkPartReceived(message.getTerrainPartData());
        if(chunkHandler.isComplete()){
            BlockChunkControl chunk = blockTerrain.getChunks()[message.getX()][message.getY()][message.getZ()];
            CubesSerializer.readFromBytes(chunk, chunkHandler.getTerrainData());
        }
    }
}
